//
// Created by yephiu on 2023/7/7.
//

#include "Nizhou_drv_RC.h"
#include "Ano_RC.h"


#define PPM_CHNUM 8


uint16_t Rc_Ppm_In[PPM_CHNUM];
uint16_t Rc_Sbus_In[16];
uint8_t sbus_flag;

static uint8_t pwminmode_irq;

/*sbus flags的结构如下所示：
flags：
bit7 = ch17 = digital channel (0x80)
bit6 = ch18 = digital channel (0x40)
bit5 = Frame lost, equivalent red LED on receiver (0x20)
bit4 = failsafe activated (0x10) b: 0001 0000
bit3 = n/a
bit2 = n/a
bit1 = n/a
bit0 = n/a*/

/**
 * SBUS数据分析函数
 * @param data 接收字节
 */
void SBUS_DataAnl(uint8_t data)
{
    static uint8_t datatmp[25];
    static uint8_t cnt = 0;

    datatmp[cnt++] = data;

    if (cnt == 25) {
        if (datatmp[0] == 0x0F && datatmp[24] == 0x00) {
            cnt = 0;
            Rc_Sbus_In[0] = (int16_t) (datatmp[2] & 0x07) << 8 | datatmp[1];
            Rc_Sbus_In[1] = (int16_t) (datatmp[3] & 0x3f) << 5 | (datatmp[2] >> 3);
            Rc_Sbus_In[2] = (int16_t) (datatmp[5] & 0x01) << 10 | ((int16_t) datatmp[4] << 2) | (datatmp[3] >> 6);
            Rc_Sbus_In[3] = (int16_t) (datatmp[6] & 0x0F) << 7 | (datatmp[5] >> 1);
            Rc_Sbus_In[4] = (int16_t) (datatmp[7] & 0x7F) << 4 | (datatmp[6] >> 4);
            Rc_Sbus_In[5] = (int16_t) (datatmp[9] & 0x03) << 9 | ((int16_t) datatmp[8] << 1) | (datatmp[7] >> 7);
            Rc_Sbus_In[6] = (int16_t) (datatmp[10] & 0x1F) << 6 | (datatmp[9] >> 2);
            Rc_Sbus_In[7] = (int16_t) datatmp[11] << 3 | (datatmp[10] >> 5);

            Rc_Sbus_In[8] = (int16_t) (datatmp[13] & 0x07) << 8 | datatmp[12];
            Rc_Sbus_In[9] = (int16_t) (datatmp[14] & 0x3f) << 5 | (datatmp[13] >> 3);
            Rc_Sbus_In[10] = (int16_t) (datatmp[16] & 0x01) << 10 | ((int16_t) datatmp[15] << 2) | (datatmp[14] >> 6);
            Rc_Sbus_In[11] = (int16_t) (datatmp[17] & 0x0F) << 7 | (datatmp[16] >> 1);
            Rc_Sbus_In[12] = (int16_t) (datatmp[18] & 0x7F) << 4 | (datatmp[17] >> 4);
            Rc_Sbus_In[13] = (int16_t) (datatmp[20] & 0x03) << 9 | ((int16_t) datatmp[19] << 1) | (datatmp[18] >> 7);
            Rc_Sbus_In[14] = (int16_t) (datatmp[21] & 0x1F) << 6 | (datatmp[20] >> 2);
            Rc_Sbus_In[15] = (int16_t) datatmp[22] << 3 | (datatmp[21] >> 5);
            sbus_flag = datatmp[23];

            if (sbus_flag & 0x10) {
                //如果有数据且能接收到有失控标记，则不处理，转嫁成无数据失控。
            } else {
                //否则有数据就喂狗
                for (uint8_t i = 0; i < 8; i++)//原RC接收程序只设计了8个通道
                {
                    ch_watch_dog_feed(i);
                }
            }
        } else {
            for (uint8_t i = 0; i < 24; i++)
                datatmp[i] = datatmp[i + 1];
            cnt = 24;
        }
    }
}


/**
 * @usage 在Ano_RC中void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)被作为处理函数被调用
 */
void PPM_DataAnl(void)
{
    static uint16_t temp_cnt1, temp_cnt1_2;

    if (TIM3->SR & TIM_IT_CC1)
    {
        ch_watch_dog_feed(CH1);//通道检测喂狗

        TIM3->SR = ~TIM_IT_CC1;//TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);
        TIM3->SR = ~TIM_FLAG_CC1OF;

        //此处删除了原来对于接收机模式的判断
        static uint8_t ch_sta = 0;
        if (GPIOC->IDR & GPIO_PIN_6)
        {
            temp_cnt1 = TIM3->CCR1;
        } else
        {
            temp_cnt1_2 = TIM3->CCR1;
            uint16_t _tmp;
            if (temp_cnt1_2 >= temp_cnt1)
                _tmp = temp_cnt1_2 - temp_cnt1;
            else
                _tmp = 0xffff - temp_cnt1 + temp_cnt1_2 + 1;
            if (_tmp > 2100 || ch_sta == PPM_CHNUM)
            {
                ch_sta = 0;
            } else
            {
                ch_watch_dog_feed(ch_sta);
                Rc_Ppm_In[ch_sta] = _tmp;//将处理的值赋值给RC_PPM_IN数组
                ch_sta++;
            }
        }
    }
}